{ "cells": [ { "cell_type": "markdown", "id": "5e72f238", "metadata": {}, "source": [ "# Tutorial 8: Manual region segmentation" ] }, { "cell_type": "markdown", "id": "e1b6ccb9", "metadata": {}, "source": [ "This tutorial demonstrates the classification of each spot based on our manual region segmentation by labelme software, and saves the region segmentation file (label.csv). Here, We use the human bronchiolar adenoma (BA) data generated by 10x Visium. The processed data can be downloaded from https://zenodo.org/record/8185216/files/Human_Bronchiolar_Adenoma.rar?download=1." ] }, { "cell_type": "markdown", "id": "e00934d3", "metadata": {}, "source": [ "## Preparation" ] }, { "cell_type": "code", "execution_count": 1, "id": "1eff6344", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")\n", "import cv2\n", "import pandas as pd\n", "import numpy as np\n", "import scanpy as sc\n", "import matplotlib.pyplot as plt\n", "import os\n", "import sys\n", "import json\n", "from stGCL.process import prefilter_genes,prefilter_specialgenes,set_seed,refine_nearest_labels\n", "from stGCL import utils" ] }, { "cell_type": "code", "execution_count": 2, "id": "ff6bd6c2", "metadata": {}, "outputs": [], "source": [ "address = \"/home/dell/stproject/stGCL/Data/Human_Bronchiolar_Adenoma/spatial/tissue_hires_image.png\"\n", "image = cv2.imread(address)\n", "f = open(\"/home/dell/stproject/stGCL/Data/Human_Bronchiolar_Adenoma/spatial/scalefactors_json.json\", 'r')\n", "\n", "content = f.read()\n", "a = json.loads(content)\n", "f.close()\n", "scale = a[\"tissue_hires_scalef\"]\n", "img_new = image.copy()" ] }, { "cell_type": "code", "execution_count": 3, "id": "680ae377", "metadata": {}, "outputs": [], "source": [ "image_loc_in = pd.read_csv('/home/dell/stproject/stGCL/Data/Human_Bronchiolar_Adenoma/spatial/tissue_positions_list.csv', \n", " header = None, index_col = 0)\n", "image_loc_in.columns=[\"isin\",\"xarray\",\"yarray\",\"x\",\"y\",]\n", "# image_loc_in[\"label\"]=\"health\"\n", "image_loc_in.loc[image_loc_in[\"isin\"]==1,\"ground_truth\"]=\"Normal\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "1ea7b104", "metadata": {}, "outputs": [], "source": [ "sh_fname = '/home/dell/stproject/stGCL/Data/Human_Bronchiolar_Adenoma/HBA_label.json'\n", "color_dict={\"Bronchus\":255,\"Tumor\":0,\"Blood vessel\":130}\n", "with open(sh_fname, 'r') as f:\n", "\tsh_json = json.load(f)\n", "Height=sh_json[\"imageHeight\"]\n", "Width=sh_json[\"imageWidth\"]" ] }, { "cell_type": "code", "execution_count": 5, "id": "44dfa156", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for sh in sh_json['shapes']:\n", " temp_celltype = sh['label']\n", " pts = np.squeeze(np.array(sh['points']))\n", " for i in pts:\n", " x = i[1] * scale* -1\n", " y = i[0] * scale* -1\n", " img_new[int(x - 4):int(x + 4), int(y - 4):int(y + 4), :] = color_dict[temp_celltype]\n", "cv2.imwrite('/home/dell/stproject/stGCL/Data/Human_Bronchiolar_Adenoma/maplabel.jpg', img_new)" ] }, { "cell_type": "markdown", "id": "1689b5f1", "metadata": {}, "source": [ "## Automatic label conversion" ] }, { "cell_type": "code", "execution_count": 6, "id": "9e44deb3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "conversion complete\n" ] } ], "source": [ "for sh in sh_json['shapes']:\n", " temp_celltype = sh['label']\n", " id = sh['group_id']\n", " pts = np.squeeze(np.array(sh['points']))\n", " for barcode, imagerow, imagecol in zip(image_loc_in.index, image_loc_in[\"x\"], image_loc_in[\"y\"]):\n", " is_in = utils.judge_in(pts, (imagecol*-1+Width+0.00001, imagerow*-1+Height+0.00001))\n", " isin = image_loc_in.loc[barcode, \"isin\"]\n", " if is_in & isin==1:\n", " image_loc_in.loc[barcode, \"ground_truth\"]=temp_celltype\n", "# print(barcode,temp_celltype)\n", "image_loc_in=image_loc_in.loc[image_loc_in[\"isin\"] == 1]\n", "image_loc_in[\"ground_truth\"].to_csv(\"/home/dell/stproject/stGCL/Data/Human_Bronchiolar_Adenoma/label.csv\")\n", "print(\"conversion complete\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.12" } }, "nbformat": 4, "nbformat_minor": 5 }